<?php

include_once '../Model/DAL/Connection.php';

class KQXS {

    public $conn;

    function __construct() {
        $con = new Connection();
        $this->conn = $con->configDB();
    }

    // SLIDES
    function GetResultBbyDateTimeNow() {
        mysqli_query($this->conn, "SET NAMES 'utf8'");
        $strQuery = mysqli_query($this->conn, 'CALL GetResultBbyDateTimeNow()');
        $arr = array();
        while ($row = mysqli_fetch_array($strQuery)) {
            array_push($arr, $row);
        }
        $this->conn->next_result();
        return $arr;
    }

    function GetResultNbyDateTimeNow() {
        mysqli_query($this->conn, "SET NAMES 'utf8'");
        $strQuery = mysqli_query($this->conn, 'CALL GetResultNbyDateTimeNow()');
        $arrN = array();
        while ($row = mysqli_fetch_array($strQuery)) {
            array_push($arrN, $row);
        }
        $this->conn->next_result();
        return $arrN;
    }

    function GetResultTbyDateTimeNow() {
        mysqli_query($this->conn, "SET NAMES 'utf8'");
        $strQuery = mysqli_query($this->conn, 'CALL GetResultTbyDateTimeNow()');
        $arrT = array();
        while ($row = mysqli_fetch_array($strQuery)) {
            array_push($arrT, $row);
        }
        $this->conn->next_result();
        return $arrT;
    }

    // TINH XO SO THEO NGAY
    function getTinhXS() {
        $dayID = $_REQUEST['dayID'];
        return $this->getProvinceByDay($dayID);
    }

    // KET QUA XO SO CHO CAC MIEN
    function getKQXS() {
        $regionID = $_REQUEST['regionID'];
        $resultDate = $_REQUEST['resultDate'];
        $province = $_REQUEST['province'];
        return $this->getResult($regionID, $resultDate, $province);
    }

    // METHOD ====================================================================== 

    function getProvinceByDay($dayID) {
        mysqli_query($this->conn, "SET NAMES 'utf8'");
        $strQuery = mysqli_query($this->conn, "CALL GetScheduleThreadsByDayID('$dayID')");
        $rowARR = array();
        while ($rows = mysqli_fetch_array($strQuery)) {
            array_push($rowARR, $rows);
        }
        return $rowARR;
    }

    function getResult($RegionID, $resultDate, $province) {
        mysqli_query($this->conn, "SET NAMES 'utf8'");
        $strQuery = NULL;
        if ($RegionID === "B") {
            $strQuery = mysqli_query($this->conn, "CALL GetResultB('$resultDate', '$province')");
        } else if ($RegionID === "T") {
            $strQuery = mysqli_query($this->conn, "CALL GetResultT('$resultDate', '$province')");
        } else if ($RegionID === "N") {
            $strQuery = mysqli_query($this->conn, "CALL GetResultN('$resultDate', '$province')");
        }
        $rows = mysqli_fetch_array($strQuery);
        return $rows;
    }

}
